package edu.ccut.saturn.studio.wfg.wfgmodel;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.search.IObserver;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher.SearchResult;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;
import edu.ccut.saturn.studio.export.createhtml.Parameters;
import edu.ccut.saturn.studio.wfg.dialog.endactivitydialog.SaturnEndActivityDialog;
import edu.ccut.saturn.studio.wfg.figure.SaturnFigure;
import edu.ccut.saturn.studio.wfg.model.Shape;

public class SaturnEndNode extends Shape {

	private static final long serialVersionUID = 1L;

//	private Image endPictureSmall = ISaturnWorkflowIcons.SMALL_END_ICON;
	private Image endPictureSmall = Activator.getImage(Images.SMALL_END_ICON);

	private String polymeMode = "1";

	private List<SaturnAttribute> logicList = new ArrayList<SaturnAttribute>();
	
	public Image getSmallIcon() {
		return endPictureSmall;
	}
	
	public void setSmallIcon(Image endPictureSmall) {
		this.endPictureSmall = endPictureSmall;
	}

	public String getPolymeMode() {
		return polymeMode;
	}

	public void setPolymeMode(String polymeMode) {
		this.polymeMode = polymeMode;
	}

	public List<SaturnAttribute> getLogicList() {
		return logicList;
	}

	public void setLogicList(List<SaturnAttribute> logicList) {
		this.logicList = logicList;
	}

	public SaturnEndNode() {
		super.setName(Messages.getString("SaturnEndNode.Finish"));    //$NON-NLS-1$
		super.setSize(new Dimension(50, 50));
	}

	public Image getIcon() {
		return Activator.getImage(Images.BIG_END_ICON);
	}

	@Override
	public IFigure getFigure() {
		SaturnFigure saturnFigure = new SaturnFigure();
		saturnFigure.setIcon(Activator.getImage(Images.BIG_END_ICON));
		saturnFigure.setName(super.getName());
		return saturnFigure;
	}

	@Override
	public Node doRefresh(Document document) {
		Node endNode = document.createElement(SaturnName.XML_NAME);
		Node rootNode = document.getDocumentElement();
		SaturnW3CUtil.createChildNode(endNode, SaturnName.KEY_NODE_ELEMENT_POLYMEMODE, this.polymeMode);
		super.createNodes(document, endNode, this.getLogicList(),
				SaturnName.KEY_NODE_ELEMENT_LOGICKEY, SaturnName.KEY_NODE_NAME_KEY);
		SaturnW3CUtil.createAttribute(endNode, SaturnName.XML_TYPE, SaturnName.KEY_NODE_ATTRIBUTE_END);
		rootNode.appendChild(endNode);
		return endNode;
	}

	public Dialog getDialog() {
		SaturnEndActivityDialog dialog = new SaturnEndActivityDialog(Display
				.getDefault().getShells()[0], this);
		return dialog;
	}

	protected Node createChildNode(Document document, SaturnAttribute attrib,
			String name) {
		return attrib.creatNode(document, name);
	}
	
//	public Map<String, Document> keyMapInProject() {
//		return ImplMap.mapfullkeyInProject(super.findTheProject(), SaturnName.KEY_NODE_NAME_LOGIC,
//				SaturnName.KEY_NODE_ELEMENT_INFORMATION, SaturnName.KEY_NODE_NAME_KEY);
//	}

	@Override
	public Shape cloneShape(Shape shape) {
		SaturnEndNode saturnEndNode = (SaturnEndNode)shape;
		SaturnEndNode newSaturnEndNode = new SaturnEndNode();
		newSaturnEndNode.setName(saturnEndNode.getName());
		newSaturnEndNode.setId(saturnEndNode.getId());
		newSaturnEndNode.setNextId(saturnEndNode.getNextId());
		newSaturnEndNode.setDescription(saturnEndNode.getDescription());
		newSaturnEndNode.setPolymeMode(saturnEndNode.getPolymeMode());
		newSaturnEndNode.setLogicList(saturnEndNode.getLogicList());
		newSaturnEndNode.setSmallIcon(saturnEndNode.getSmallIcon());
		return newSaturnEndNode;
	}
	
	
public List<SearchResult> keyMapInWorkspace(IObserver observer) {
		
		List<SearchResult> list = null;
		
		try {
			list = SaturnFileSearcher.getValueAndDocumentMapInWorkspace("logic" , observer, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	
	public List<SearchResult> keyMapInProject(IObserver observer) {
		List<SearchResult> list = null;
		
		try {
			list = SaturnFileSearcher.getValueAndDocumentMapInProject(super.findTheProject(), "logic",
					observer, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public Parameters createNodeHtml(String filePath) {
		return null;
	}
}